package com.fly.caihong.activity;

import android.os.Environment;
import android.view.View;
import android.widget.TextView;

import com.fly.caihong.R;
import com.fly.caihong.base.BaseActivity;
import com.fly.caihong.utils.DataCacheMgr;
import com.fly.caihong.utils.FileUtils;
import com.fly.caihong.utils.ToastUtils;

public class SettingActivity extends BaseActivity {

    private View mCacheClear, mDataClear;
    private TextView mTvCacheSize;

    @Override
    protected void initViews() {
        super.initViews();
        mCacheClear = findViewById(R.id.fl_clear_cache);
        mTvCacheSize = findViewById(R.id.tv_cache_size);
        mDataClear = findViewById(R.id.tv_clear_data);
        mToolbar.setMainTitle("设置");
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initData() {
        super.initData();
        getCacheSize();
    }

    private void getCacheSize() {
        try {
            mTvCacheSize.setText(DataCacheMgr.getTotalCacheSize(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initListeners() {
        super.initListeners();
        mCacheClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataCacheMgr.clearAllCache(SettingActivity.this);
                getCacheSize();
            }
        });
        mDataClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FileUtils.deleteAllInDir(Environment.getExternalStorageDirectory() + "/caihong");
                ToastUtils.showLong("清除成功，请退出应用后重启");
            }
        });
    }
}
